from django.shortcuts import render
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from areas.models import Area
from areas.serializers import AreaListSerializer, AreaRetrieveSerializer


# class AreaListView(ListAPIView):
#     """先查省的信息"""
#     queryset = Area.objects.filter(parent_id__isnull=True)
#     serializer_class = AreaListSerializer
#
#
# class AreaRetrieveView(RetrieveAPIView):
#     """通过省的id查下面的所有市和区"""
#     queryset = Area.objects.all()
#     serializer_class = AreaRetrieveSerializer


class AreaViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent_id__isnull=True)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaListSerializer
        else:
            return AreaRetrieveSerializer

